// 隐患排查单添加（排查人）
const app = getApp()
const { addApi } = require('../../../api/services/yhpcAdd/index')
const util = require('../../../utils/util.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    taskId: '',
    comName: '',
    comId: '',
    checkDate: '',
    checkName: '',
    escortName: '',
    escortTel: '',
    limitDate: '',
    content: '',
    checkUrl: '',
    show1: false,
    show: false,
    tellError: '',
    submitBtnFlag: true,

    currentDate: new Date().getTime(),
    minDate: new Date().getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`
      }
      if (type === 'month') {
        return `${value}月`
      }
      return value
    },
    isPage: false, //禁止滚动
  },
  // 企业选择
  handleComName() {
    wx.navigateTo({
      url: '/pages/safety/searchCompany/searchCompany',
    })
  },

  //  排查日期
  handlePcTime() {
    this.setData({ show1: true })
  },

  // 整改期限
  showPopup() {
    this.setData({ show: true })
  },

  // 时间弹窗
  onCloseTime() {
    this.setData({ show: false, show1: false })
  },

  // 排查日期确定
  handleConfirm1(event) {
    this.setData({
      checkDate: util.formatTime(new Date(event.detail)),
      show1: false,
    })
  },

  // 整改期限确定
  handleConfirm(event) {
    this.setData({
      limitDate: util.formatTime(new Date(event.detail)),
      show: false,
    })
  },

  // 排查人
  onChangeCheckName(event) {
    this.setData({ checkName: event.detail })
  },

  // 现场陪同人
  onChangeEscortName(event) {
    this.setData({ escortName: event.detail })
  },

  // 现场陪同人电话
  onChangeEscortTel(event) {
    this.setData({ escortTel: event.detail })
  },

  // 手机号码效验
  validateTell(event) {
    const reg = /^[1][3,4,5,7,8][0-9]{9}$/
    let tell = event.detail.value
    if (reg.test(tell)) {
      this.setData({
        tellError: '',
        submitBtnFlag: true,
      })
    } else {
      this.setData({
        tellError: '手机号码格式错误',
        submitBtnFlag: false,
      })
    }
  },

  // 本次服务内容
  onChangeContent(event) {
    this.setData({ content: event.detail })
  },

  async handleSubmit() {
    let params = {
      taskId: this.data.taskId,
      comName: this.data.comName,
      comId: this.data.comId,
      checkDate: this.data.checkDate,
      checkName: this.data.checkName,
      escortName: this.data.escortName,
      escortTel: this.data.escortTel,
      limitDate: this.data.limitDate,
      checkUrl: this.data.checkUrl,
      checkTel: '',
      content: this.data.content,
      address: '',
      creditNo: '',
    }

    console.log(params)
    const res = await addApi(params)
    if (res.code != 200) {
      return app.toast(res.msg)
    }
    wx.navigateBack({
      delta: 1, //返回上一级页面
    })
  },

  //  排查人签名
  signatureFn1() {
    wx.navigateTo({
      url: '/pages/safety/signature/signature?state=checkUrl',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let itemObj = options.item
    console.log(itemObj)
    if (itemObj) {
      this.setData({ comName: itemObj.comName, comId: itemObj.comId })
    }
  },
  onShow: function () {
    this.setData({ taskId: app.globalData.taskId, comName: app.globalData.comName, comId: app.globalData.comId })

    let checkName = wx.getStorageSync('nickName')

    this.setData({ checkName: checkName })
  },
})
